package com.jeesite.modules.sys.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.Organization;
import com.jeesite.modules.sys.service.support.OrganizationService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 组织账套Controller
 * @author 码脑洞
 * @version 2025-04-07
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/organization")
public class OrganizationController extends BaseController {

	@Autowired
	private OrganizationService organizationService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Organization get(Long autoId, boolean isNewRecord) {
		return organizationService.get(autoId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("sys:organization:view")
	@RequestMapping(value = {"list", ""})
	public String list(Organization organization, Model model) {
		model.addAttribute("organization", organization);
		return "modules/sys/organizationList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("sys:organization:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Organization> listData(Organization organization, HttpServletRequest request, HttpServletResponse response) {
		organization.setPage(new Page<>(request, response));
		Page<Organization> page = organizationService.findPage(organization);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("sys:organization:view")
	@RequestMapping(value = "form")
	public String form(Organization organization, Model model) {
		model.addAttribute("organization", organization);
		return "modules/sys/organizationForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("sys:organization:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Organization organization) {
		// 校验组织编码是否已存在
		if (organizationService.isOrgCodeExist(organization.getOrgCode(),
				organization.getIsNewRecord() ? null : organization.getOrgCode())) {
			return renderResult(Global.FALSE, text("组织编码已存在，请重新输入！"));
		}
		organizationService.save(organization);
		return renderResult(Global.TRUE, text("保存组织账套成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("sys:organization:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Organization organization) {
		organization.setStatus(Organization.STATUS_DISABLE);
		organizationService.updateStatus(organization);
		return renderResult(Global.TRUE, text("停用组织账套成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("sys:organization:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Organization organization) {
		organization.setStatus(Organization.STATUS_NORMAL);
		organizationService.updateStatus(organization);
		return renderResult(Global.TRUE, text("启用组织账套成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("sys:organization:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Organization organization) {
		organizationService.delete(organization);
		return renderResult(Global.TRUE, text("删除组织账套成功！"));
	}



	/**
	 * 校验组织编码是否已存在
	 */
	@RequiresPermissions("sys:organization:edit")
	@RequestMapping(value = "checkOrgCode")
	@ResponseBody
	public String checkOrgCode(String orgCode, String oldOrgCode) {
		if (orgCode != null && orgCode.equals(oldOrgCode)) {
			return Global.TRUE;
		}
		if (orgCode != null && !organizationService.isOrgCodeExist(orgCode, oldOrgCode)) {
			return Global.TRUE;
		}
		return Global.FALSE;
	}
	
}